package com.galaxy.device.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.core.exception.ServiceException;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.device.domain.DeviceChannel;
import com.galaxy.device.domain.DeviceStock;
import com.galaxy.device.domain.bo.DeviceChannelBo;
import com.galaxy.device.domain.convert.DeviceChannelConvert;
import com.galaxy.device.domain.vo.DeviceChannelVo;
import com.galaxy.device.mapper.DeviceChannelMapper;
import com.galaxy.device.mapper.DeviceStockMapper;
import com.galaxy.device.service.IDeviceChannelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 设备通道服务实现类
 */
@RequiredArgsConstructor
@Service
public class DeviceChannelServiceImpl extends ServiceImpl<DeviceChannelMapper, DeviceChannel> implements IDeviceChannelService {

    private final DeviceChannelMapper deviceChannelMapper;

    private final DeviceStockMapper deviceStockMapper;

    @DS("slave")
    @Override
    public List<DeviceChannelVo> queryList(DeviceChannelBo bo) {
        LambdaQueryWrapper<DeviceChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getDeviceId() != null, DeviceChannel::getDeviceId, bo.getDeviceId());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelNo()), DeviceChannel::getChannelNo, bo.getChannelNo());
        lqw.eq(StringUtils.isNotBlank(bo.getChannelPosition()), DeviceChannel::getChannelPosition, bo.getChannelPosition());
        lqw.eq(bo.getChannelType() != null, DeviceChannel::getChannelType, bo.getChannelType());
        lqw.eq(bo.getStatus() != null, DeviceChannel::getStatus, bo.getStatus());
        List<DeviceChannel> list = deviceChannelMapper.selectList(lqw);
        return DeviceChannelConvert.INSTANCE.convertVoList(list);
    }

    @DS("slave")
    @Override
    public DeviceChannelVo getInfo(Long id) {
        DeviceChannel channel = deviceChannelMapper.selectById(id);
        if (channel == null) {
            throw new ServiceException("设备通道信息不存在");
        }
        return DeviceChannelConvert.INSTANCE.convertVo(channel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(DeviceChannelBo bo) {
        DeviceChannel channel = DeviceChannelConvert.INSTANCE.convert(bo);
        deviceChannelMapper.insert(channel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DeviceChannelBo bo) {
        DeviceChannel channel = deviceChannelMapper.selectById(bo.getId());
        if (channel == null) {
            throw new ServiceException("设备通道信息不存在");
        }
        deviceStockMapper.update(
            new LambdaUpdateWrapper<DeviceStock>()
                .eq(DeviceStock::getChannelId, channel.getId())
                .set(DeviceStock::getMaxQuantity, bo.getMaxCapacity()));
        MapstructUtils.convert(bo, channel);
        deviceChannelMapper.updateById(channel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long id) {
        deviceChannelMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deviceChannelMapper.deleteById(id);
        }
    }

    @Override
    public List<DeviceChannelVo> queryByDeviceId(Long deviceId) {
        LambdaQueryWrapper<DeviceChannel> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DeviceChannel::getDeviceId, deviceId);
        List<DeviceChannel> list = deviceChannelMapper.selectList(lqw);
        return DeviceChannelConvert.INSTANCE.convertVoList(list);
    }
}
